#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <cm/img/cm_img.h>
#include <cm/character/character.h>
#include <cm/character/lattice.h>
#include <cm/file/cm_file.h>
#include <cm/img/cm_yuv420sp.h>

/* 注意释放！！ */
static char *get_gb2312_str(const char *utf8)
{
	size_t len = strlen(utf8) + 1;
	if (len <= 0)
		return NULL;

	char *utf = calloc(1, len);
	if (utf == NULL)
		return NULL;
	char *src = utf;
	strncpy(utf, utf8, len);

	char *gb2312 = calloc(1, len);
	char *dst = gb2312;
	if (gb2312 != NULL) {
		size_t rlen = len;
		if (0 > UTF82GB2312(&src, &len, &dst, &rlen)) {
			free(gb2312);
			gb2312 = NULL;
		}
	}
	free(utf);
	return gb2312;
}

int main(int argc, char **argv)
{
	lattice16_init("../character/hzk16", "../character/ASC16");
	char *data;
	size_t size = yuv420sp_data_total(2560, 2048);
	data = calloc(1, size);
	if (data == NULL)
		return 0;

	if (cm_fread("./1.yuv", data, size) <= 0)
		NULL; //return 0; // 不返回可以生成一幅全是0的图像

	struct draw_lattice_param param;
	struct rgbcolor rgb = {255,0,0};
	RGBColor2YUVColor(&rgb, &(param.color.yuv));

	param.type = CM_COLOR_YUV420SP;
	param.img = (unsigned char *) data;
	param.w = 2560;
	param.h = 2048;
	param.x = 100;
	param.y = 100;
	param.str = get_gb2312_str("这是一个打字的测试ABCDEFGHIJKLMOPQRSTUVWXYZ1234567890");
	param.scale = 4;

	draw_lattice(&param);

	cm_fwrite("./2.yuv", data, size, 0);
	return 0;
}

